/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: SetTest
 * Author:   AEO WUGUOZHI
 * Date:     2020/7/22 15:18
 * Description: Set
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package collection;

import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * 〈一句话功能简述〉<br> 
 * 〈Set〉
 *
 * @author AEO WUGUOZHI
 * @create 2020/7/22
 * @since 1.0.0
 */
public class SetTest {

    public List<Course> coursesToSelect;

    private Scanner console;

    public Student student;

    public SetTest(){
        coursesToSelect = new ArrayList<Course>();
        console = new Scanner(System.in);
    }

    public void testAdd(){
        Course cr1 = new Course("1","数据结构");
        coursesToSelect.add(cr1);
        Course temp = (Course)coursesToSelect.get(0);
//        System.out.println("课程id：" + temp.getId() + "  课程名称：" + temp.getName());

        Course cr2 = new Course("2","C语言");
        coursesToSelect.add(0,cr2);
        temp = (Course)coursesToSelect.get(0);
//        System.out.println("课程id：" + temp.getId() + "  课程名称：" + temp.getName());

//        coursesToSelect.add(cr1);
//        Course temp0 = (Course)coursesToSelect.get(2);
//        System.out.println("课程id：" + temp0.getId() + "  课程名称：" + temp0.getName());

//        Course cr3 = new Course("3","设计模式");           //抛出异常，数组小标越界
//        coursesToSelect.add(3,cr3);

        Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")};
        coursesToSelect.addAll(Arrays.asList(course));

        Course temp3 = (Course)coursesToSelect.get(2);
        Course temp4 = (Course)coursesToSelect.get(3);
//        System.out.println("添加了两门课程" + temp3.getId() + ":" + temp3.getName() + "\n" + temp4.getId() + ":" + temp4.getName());

        Course[] course2 = {new Course("5","高等数学"),new Course("6","大学英语")};
        coursesToSelect.addAll(2,Arrays.asList(course2));
        Course temp5 = (Course)coursesToSelect.get(2);
        Course temp6 = (Course)coursesToSelect.get(3);
//        System.out.println("添加了两门课程" + temp5.getId() + ":" + temp5.getName() + "\n" + temp6.getId() + ":" + temp6.getName());
    }

    public void testForEach(){
        System.out.println("有如下课程待选(通过foreach()访问)");
        for(Object obj:coursesToSelect){
            Course cr = (Course)obj;
            System.out.println("课程："+cr.getId() + "：" + cr.getName());
        }
    }

    public void testForEachForSet(Student student){
        //打印输出，学生所选的课程
        System.out.println("共选择了：" + student.courses.size() + "门课程！");
        for(Course cr : student.courses){
            System.out.println("选择了课程：" + cr.getId() + ":" + cr.getName());
        }
    }

    /*
    * 测试List的Contains()方法
    *
    * */

    public void testListContains(){
        //取得备选课程序列的第0个元素
        Course course = coursesToSelect.get(0);
        //打印CoursesToSelected是否包含course对象
        System.out.println("取得课程：" + course.getName());
        System.out.println("备选课程中是否包含课程：" + course.getName() + "," + coursesToSelect.contains(course));
        //提示输入课程名称
        System.out.println("请输入课程名称：");
        String name = console.next();
        //创建一个新的课程对象，ID和名称，与course对象完全一样。
        Course course2 = new Course();
        course2.name = name;
        System.out.println("新创建课程：" + course2.name + "id:" + course2.getId());
        System.out.println("备选课程中是否包含课程：" + course2.getName() + "," + coursesToSelect.contains(course2));
        //通过indexOf方法来获取某元素的索引位置
        if(coursesToSelect.contains(course2)){
            System.out.println("课程：" + course2.name + "的索引位置为：" + coursesToSelect.indexOf(course2));;
        }
    }
/*
* 创建学生对象并且选课
* */
    public  void createStudentStudentAndSelectCours(){
        //创建一个学生对象
        student = new Student("1","小明");
        System.out.println("欢迎学生：" + student.getName() + "选课!");
        //创建一个Scanner对象，用来接受从键盘输入的课程ID
        Scanner console = new Scanner(System.in);

        for(int i = 0;i < 3;i++){
            System.out.println("请输入课程ID");
            String courseID = console.next();
            for(Course cr : coursesToSelect){
                if(cr.getId().equals(courseID)){
                    student.courses.add(cr);
/**
 * Set中，添加某个对象，无论添加多少次，最终只会保留一个该对象(的引用),
 * 并且，保留的是第一次添加的那一个
 * */
                    student.courses.add(cr);
                    student.courses.add(null);
                }
            }
        }
    }

    /*
    * 测试Set的contains方法
    * */
    public void testSetContains(){
        // 提示输入课程名称
        System.out.println("请输入学生已选的课程名称：");
        String name = console.next();
        //创建一个新的课程对象，ID和名称，与course对象完全一样。
        Course course2 = new Course();
        course2.name = name;
        System.out.println("新创建课程：" + course2.name + "id:" + course2.getId());
        System.out.println("备选课程中是否包含课程：" + course2.getName() + "," + student.courses.contains(course2));
    }

    public static void main(String[] args) {
        SetTest st = new SetTest();
        st.testAdd();
        st.testListContains();
        st.testForEach();
//        st.createStudentStudentAndSelectCours();
//        st.testSetContains();
//        st.testListContains();

//
//        st.testForEachForSet(student);

    }
}